home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pine / imap-3.0 / ANSI / c-client / rfc822.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-06  |  50.1 KB  |  1,515 lines

  1. /*
  2.  * Program:    RFC-822 routines (originally from SMTP)
  3.  *
  4.  * Author:    Mark Crispin
  5.  *        Networks and Distributed Computing
  6.  *        Computing & Communications
  7.  *        University of Washington
  8.  *        Administration Building, AG-44
  9.  *        Seattle, WA  98195
  10.  *        Internet: MRC@CAC.Washington.EDU
  11.  *
  12.  * Date:    27 July 1988
  13.  * Last Edited:    6 July 1993
  14.  *
  15.  * Sponsorship:    The original version of this work was developed in the
  16.  *        Symbolic Systems Resources Group of the Knowledge Systems
  17.  *        Laboratory at Stanford University in 1987-88, and was funded
  18.  *        by the Biomedical Research Technology Program of the National
  19.  *        Institutes of Health under grant number RR-00785.
  20.  *
  21.  * Original version Copyright 1988 by The Leland Stanford Junior University.
  22.  * Copyright 1993 by the University of Washington.
  23.  *
  24.  *  Permission to use, copy, modify, and distribute this software and its
  25.  * documentation for any purpose and without fee is hereby granted, provided
  26.  * that the above copyright notices appear in all copies and that both the
  27.  * above copyright notices and this permission notice appear in supporting
  28.  * documentation, and that the name of the University of Washington or The
  29.  * Leland Stanford Junior University not be used in advertising or publicity
  30.  * pertaining to distribution of the software without specific, written prior
  31.  * permission.  This software is made available "as is", and
  32.  * THE UNIVERSITY OF WASHINGTON AND THE LELAND STANFORD JUNIOR UNIVERSITY
  33.  * DISCLAIM ALL WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD TO THIS SOFTWARE,
  34.  * INCLUDING WITHOUT LIMITATION ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  35.  * FITNESS FOR A PARTICULAR PURPOSE, AND IN NO EVENT SHALL THE UNIVERSITY OF
  36.  * WASHINGTON OR THE LELAND STANFORD JUNIOR UNIVERSITY BE LIABLE FOR ANY
  37.  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  38.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
  39.  * CONTRACT, TORT (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF
  40.  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  41.  *
  42.  */
  43.  
  44.  
  45. #include <ctype.h>
  46. #include <stdio.h>
  47. #include <time.h>
  48. #include "mail.h"
  49. #include "osdep.h"
  50. #include "rfc822.h"
  51. #include "misc.h"
  52.  
  53. /* RFC-822 static data */
  54.  
  55.  
  56. /* Body formats constant strings, must match definitions in mail.h */
  57.  
  58. const char *body_types[] = {    /* defined body type strings */
  59.   "TEXT", "MULTIPART", "MESSAGE", "APPLICATION", "AUDIO", "IMAGE", "VIDEO",
  60.   "X-UNKNOWN"
  61. };
  62.  
  63.  
  64. const char *body_encodings[] = {/* defined body encoding strings */
  65.   "7BIT", "8BIT", "BINARY", "BASE64", "QUOTED-PRINTABLE", "X-UNKNOWN"
  66. };
  67.  
  68.  
  69. /* Token delimiting special characters */
  70.  
  71.                 /* full RFC-822 specials */
  72. const char *rspecials =  "()<>@,;:\\\"[].";
  73.                 /* body token specials */
  74. const char *tspecials = " ()<>@,;:\\\"[]./?=";
  75.  
  76.  
  77. /* Once upon a time, CSnet had a mailer which assigned special semantics to
  78.  * dot in e-mail addresses.  For the sake of that mailer, dot was added to
  79.  * the RFC-822 definition of `specials', even though it had numerous bad side
  80.  * effects:
  81.  *   1)    It broke mailbox names on systems which had dots in user names, such as
  82.  *    Multics and TOPS-20.  RFC-822's syntax rules require that `Admin . MRC'
  83.  *    be considered equivalent to `Admin.MRC'.  Fortunately, few people ever
  84.  *    tried this in practice.
  85.  *   2) It required that all personal names with an initial be quoted, a widely
  86.  *    detested user interface misfeature.
  87.  *   3)    It made the parsing of host names be non-atomic for no good reason.
  88.  * To work around these problems, the following alternate specials lists are
  89.  * defined.  hspecials and wspecials are used in lieu of rspecials, and
  90.  * ptspecials are used in lieu of tspecials.  These alternate specials lists
  91.  * make the parser work a lot better in the real world.  It ain't politically
  92.  * correct, but it lets the users get their job done!
  93.  */
  94.  
  95.                 /* parse-host specials */
  96. const char *hspecials = " ()<>@,;:\\\"";
  97.                 /* parse-word specials */
  98. const char *wspecials = " ()<>@,;:\\\"[]";
  99.                 /* parse-token specials for parsing */
  100. const char *ptspecials = " ()<>@,;:\\\"[]/?=";
  101.  
  102. /* RFC822 writing routines */
  103.  
  104.  
  105. /* Write RFC822 header from message structure
  106.  * Accepts: scratch buffer to write into
  107.  *        message envelope
  108.  *        message body
  109.  */
  110.  
  111. void rfc822_header (char *header,ENVELOPE *env,BODY *body)
  112. {
  113.   if (env->remail) {        /* if remailing */
  114.     long i = strlen (env->remail);
  115.                 /* flush extra blank line */
  116.     if (i > 4 && env->remail[i-4] == '\015') env->remail[i-2] = '\0';
  117.     strcpy (header,env->remail);/* start with remail header */
  118.   }
  119.   else *header = '\0';        /* else initialize header to null */
  120.   rfc822_header_line (&header,"Newsgroups",env,env->newsgroups);
  121.   rfc822_header_line (&header,"Date",env,env->date);
  122.   rfc822_address_line (&header,"From",env,env->from);
  123.   rfc822_address_line (&header,"Sender",env,env->sender);
  124.   rfc822_address_line (&header,"Reply-To",env,env->reply_to);
  125.   rfc822_header_line (&header,"Subject",env,env->subject);
  126.   rfc822_address_line (&header,"To",env,env->to);
  127.   rfc822_address_line (&header,"cc",env,env->cc);
  128. /* bcc's are never written...
  129.  * rfc822_address_line (&header,"bcc",env,env->bcc);
  130.  */
  131.   rfc822_header_line (&header,"In-Reply-To",env,env->in_reply_to);
  132.   rfc822_header_line (&header,"Message-ID",env,env->message_id);
  133.   if (body && !env->remail) {    /* not if remail or no body structure */
  134.     strcat (header,"MIME-Version: 1.0\015\012");
  135.     rfc822_write_body_header (&header,body);
  136.   }
  137.   strcat (header,"\015\012");    /* write terminating blank line */
  138. }
  139.  
  140. /* Write RFC822 address from header line
  141.  * Accepts: pointer to destination string pointer
  142.  *        pointer to header type
  143.  *        message to interpret
  144.  *        address to interpret
  145.  */
  146.  
  147. void rfc822_address_line (char **header,char *type,ENVELOPE *env,ADDRESS *adr)
  148. {
  149.   char *t,tmp[MAILTMPLEN];
  150.   long i,len,n = 0;
  151.   char *s = (*header += strlen (*header));
  152.   if (adr) {            /* do nothing if no addresses */
  153.     if (env && env->remail) strcat (s,"ReSent-");
  154.     strcat (s,type);        /* write header name */
  155.     strcat (s,": ");
  156.     s += (len = strlen (s));    /* initial string length */
  157.     do {            /* run down address list */
  158.       *(t = tmp) = '\0';    /* initially empty string */
  159.                 /* start of group? */
  160.       if (adr->mailbox && !adr->host) {
  161.                 /* yes, write group name */
  162.     rfc822_cat (t,adr->mailbox,rspecials);
  163.     strcat (t,": ");    /* write group identifier */
  164.     n++;            /* in a group, suppress expansion */
  165.       }
  166.       else {            /* not start of group */
  167.     if (!adr->host && n) {    /* end of group? */
  168.       strcat (t,";");    /* write close delimiter */
  169.       n--;            /* no longer in a group */
  170.     }
  171.     else if (!n) {        /* only print if not inside a group */
  172.                 /* simple case? */
  173.       if (!(adr->personal || adr->adl)) rfc822_address (t,adr);
  174.       else {        /* no, must use phrase <route-addr> form */
  175.         if (adr->personal) rfc822_cat (t,adr->personal,rspecials);
  176.         strcat (t," <");    /* write address delimiter */
  177.                 /* write address */
  178.         rfc822_address (t,adr);
  179.         strcat (t,">");    /* closing delimiter */
  180.       }
  181.     }
  182.                 /* write delimiter for next recipient */
  183.     if (!n && adr->next && adr->next->mailbox) strcat (t,", ");
  184.       }
  185.                 /* if string would overflow */
  186.       if ((len += (i = strlen (t))) > 78) {
  187.     len = 4 + i;        /* continue it on a new line */
  188.     *s++ = '\015'; *s++ = '\012';
  189.     *s++ = ' '; *s++ = ' '; *s++ = ' '; *s++ = ' ';
  190.       }
  191.       while (*t) *s++ = *t++;    /* write this address */
  192.     } while (adr = adr->next);
  193.                 /* tie off header line */
  194.     *s++ = '\015'; *s++ = '\012'; *s = '\0';
  195.     *header = s;        /* set return value */
  196.   }
  197. }
  198.  
  199. /* Write RFC822 text from header line
  200.  * Accepts: pointer to destination string pointer
  201.  *        pointer to header type
  202.  *        message to interpret
  203.  *        pointer to text
  204.  */
  205.  
  206. void rfc822_header_line (char **header,char *type,ENVELOPE *env,char *text)
  207. {
  208.   if (text) sprintf ((*header += strlen (*header)),"%s%s: %s\015\012",
  209.              env->remail ? "ReSent-" : "",type,text);
  210. }
  211.  
  212.  
  213. /* Write RFC822 address
  214.  * Accepts: pointer to destination string
  215.  *        address to interpret
  216.  */
  217.  
  218. void rfc822_write_address (char *dest,ADDRESS *adr)
  219. {
  220.   while (adr) {
  221.                 /* start of group? */
  222.     if (adr->mailbox && !adr->host) {
  223.                 /* yes, write group name */
  224.       rfc822_cat (dest,adr->mailbox,rspecials);
  225.       strcat (dest,": ");    /* write group identifier */
  226.       adr = adr->next;        /* move to next address block */
  227.     }
  228.     else {            /* end of group? */
  229.       if (!adr->host) strcat (dest,";");
  230.                 /* simple case? */
  231.       else if (!(adr->personal || adr->adl)) rfc822_address (dest,adr);
  232.       else {            /* no, must use phrase <route-addr> form */
  233.     if (adr->personal) rfc822_cat (dest,adr->personal,rspecials);
  234.     strcat (dest," <");    /* write address delimiter */
  235.     rfc822_address (dest,adr);/* write address */
  236.     strcat (dest,">");    /* closing delimiter */
  237.       }
  238.                 /* delimit if there is one */
  239.       if ((adr = adr->next) && adr->mailbox) strcat (dest,", ");
  240.     }
  241.   }
  242. }
  243.  
  244. /* Write RFC822 route-address to string
  245.  * Accepts: pointer to destination string
  246.  *        address to interpret
  247.  */
  248.  
  249. void rfc822_address (char *dest,ADDRESS *adr)
  250. {
  251.   if (adr && adr->host) {    /* no-op if no address */
  252.     if (adr->adl) {        /* have an A-D-L? */
  253.       strcat (dest,adr->adl);
  254.       strcat (dest,":");
  255.     }
  256.                 /* write mailbox name */
  257.     rfc822_cat (dest,adr->mailbox,wspecials);
  258.     strcat (dest,"@");        /* host delimiter */
  259.     strcat (dest,adr->host);    /* write host name */
  260.   }
  261. }
  262.  
  263.  
  264. /* Concatenate RFC822 string
  265.  * Accepts: pointer to destination string
  266.  *        pointer to string to concatenate
  267.  *        list of special characters
  268.  */
  269.  
  270. void rfc822_cat (char *dest,char *src,const char *specials)
  271. {
  272.   char *s;
  273.   if (strpbrk (src,specials)) {    /* any specials present? */
  274.     strcat (dest,"\"");        /* opening quote */
  275.                 /* truly bizarre characters in there? */
  276.     while (s = strpbrk (src,"\\\"")) {
  277.       strncat (dest,src,s-src);    /* yes, output leader */
  278.       strcat (dest,"\\");    /* quoting */
  279.       strncat (dest,s,1);    /* output the bizarre character */
  280.       src = ++s;        /* continue after the bizarre character */
  281.     }
  282.     if (*src) strcat (dest,src);/* output non-bizarre string */
  283.     strcat (dest,"\"");        /* closing quote */
  284.   }
  285.   else strcat (dest,src);    /* otherwise it's the easy case */
  286. }
  287.  
  288. /* Write body content header
  289.  * Accepts: pointer to destination string pointer
  290.  *        pointer to body to interpret
  291.  */
  292.  
  293. void rfc822_write_body_header (char **dst,BODY *body)
  294. {
  295.   char *s;
  296.   PARAMETER *param = body->parameter;
  297.   sprintf (*dst += strlen (*dst),"Content-Type: %s",body_types[body->type]);
  298.   s = body->subtype ? body->subtype : rfc822_default_subtype (body->type);
  299.   sprintf (*dst += strlen (*dst),"/%s",s);
  300.   if (param) do {
  301.     sprintf (*dst += strlen (*dst),"; %s=",param->attribute);
  302.     rfc822_cat (*dst,param->value,tspecials);
  303.   } while (param = param->next);
  304.   else if (body->type == TYPETEXT) strcat (*dst,"; charset=US-ASCII");
  305.   strcpy (*dst += strlen (*dst),"\015\012");
  306.   if (body->encoding)        /* note: encoding 7BIT never output! */
  307.     sprintf (*dst += strlen (*dst),"Content-Transfer-Encoding: %s\015\012",
  308.          body_encodings[body->encoding]);
  309.   if (body->id) sprintf (*dst += strlen (*dst),"Content-ID: %s\015\012",
  310.              body->id);
  311.   if (body->description)
  312.     sprintf (*dst += strlen (*dst),"Content-Description: %s\015\012",
  313.          body->description);
  314. }
  315.  
  316.  
  317. /* Subtype defaulting (a no-no, but regretably necessary...)
  318.  * Accepts: type code
  319.  * Returns: default subtype name
  320.  */
  321.  
  322. char *rfc822_default_subtype (unsigned short type)
  323. {
  324.   switch (type) {
  325.   case TYPETEXT:        /* default is TEXT/PLAIN */
  326.     return "PLAIN";
  327.   case TYPEMULTIPART:        /* default is MULTIPART/MIXED */
  328.     return "MIXED";
  329.   case TYPEMESSAGE:        /* default is MESSAGE/RFC822 */
  330.     return "RFC822";
  331.   case TYPEAPPLICATION:        /* default is APPLICATION/OCTET-STREAM */
  332.     return "OCTET-STREAM";
  333.   case TYPEAUDIO:        /* default is AUDIO/BASIC */
  334.     return "BASIC";
  335.   default:            /* others have no default subtype */
  336.     return "UNKNOWN";
  337.   }
  338. }
  339.  
  340. /* RFC822 parsing routines */
  341.  
  342.  
  343. /* Parse an RFC822 message
  344.  * Accepts: pointer to return envelope
  345.  *        pointer to return body
  346.  *        pointer to header
  347.  *        header byte count
  348.  *        pointer to body stringstruct
  349.  *        pointer to local host name
  350.  *        pointer to scratch buffer
  351.  */
  352.  
  353. void rfc822_parse_msg (ENVELOPE **en,BODY **bdy,char *s,unsigned long i,
  354.                STRING *bs,char *host,char *tmp)
  355. {
  356.   char c,*t,*d;
  357.   ENVELOPE *env = (*en = mail_newenvelope ());
  358.   BODY *body = bdy ? (*bdy = mail_newbody ()) : NIL;
  359.   long MIMEp = NIL;        /* flag that MIME semantics are in effect */
  360.   while (i > 0 && *s != '\n') {    /* until end of header */
  361.     t = tmp;            /* initialize buffer pointer */
  362.     c = ' ';            /* and previous character */
  363.     while (c) {            /* collect text until logical end of line */
  364.       switch (*s) {
  365.       case '\n':        /* newline, possible end of logical line */
  366.                 /* tie off unless next line starts with WS */
  367.     if (s[1] != ' ' && s[1] != '\t') *t = c = '\0';
  368.     break;
  369.       case '\015':        /* return, unlikely but just in case */
  370.       case '\t':        /* tab */
  371.       case ' ':            /* here for all forms of whitespace */
  372.                 /* insert whitespace if not already there */
  373.     if (c != ' ') *t++ = c = ' ';
  374.     break;
  375.       default:            /* all other characters */
  376.     *t++ = c = *s;        /* insert the character into the line */
  377.     break;
  378.       }
  379.       if (--i > 0) s++;        /* get next character */
  380.       else *t++ = c = '\0';    /* end of header */
  381.     }
  382.  
  383.                 /* find header item type */
  384.     if (t = d = strchr (tmp,':')) {
  385.       *d++ = '\0';        /* tie off header item, point at its data */
  386.       while (*d == ' ') d++;    /* flush whitespace */
  387.       while ((tmp < t--) && (*t == ' ')) *t = '\0';
  388.       switch (*ucase (tmp)) {    /* dispatch based on first character */
  389.       case '>':            /* possible >From: */
  390.     if (!strcmp (tmp+1,"FROM")) rfc822_parse_adrlist (&env->from,d,host);
  391.     break;
  392.       case 'B':            /* possible bcc: */
  393.     if (!strcmp (tmp+1,"CC")) rfc822_parse_adrlist (&env->bcc,d,host);
  394.     break;
  395.       case 'C':            /* possible cc: or Content-<mumble>*/
  396.     if (!strcmp (tmp+1,"C")) rfc822_parse_adrlist (&env->cc,d,host);
  397.     else if ((tmp[1] == 'O') && (tmp[2] == 'N') && (tmp[3] == 'T') &&
  398.          (tmp[4] == 'E') && (tmp[5] == 'N') && (tmp[6] == 'T') &&
  399.          (tmp[7] == '-') && body &&
  400.          (MIMEp || (search (s-1,i,"\012MIME-Version",(long) 13))))
  401.       rfc822_parse_content_header (body,tmp+8,d);
  402.     break;
  403.       case 'D':            /* possible Date: */
  404.     if (!env->date && !strcmp (tmp+1,"ATE")) env->date = cpystr (d);
  405.     break;
  406.       case 'F':            /* possible From: */
  407.     if (!strcmp (tmp+1,"ROM")) rfc822_parse_adrlist (&env->from,d,host);
  408.     break;
  409.       case 'I':            /* possible In-Reply-To: */
  410.     if (!env->in_reply_to && !strcmp (tmp+1,"N-REPLY-TO"))
  411.       env->in_reply_to = cpystr (d);
  412.     break;
  413.  
  414.       case 'M':            /* possible Message-ID: or MIME-Version: */
  415.     if (!env->message_id && !strcmp (tmp+1,"ESSAGE-ID"))
  416.       env->message_id = cpystr (d);
  417.     else if (!strcmp (tmp+1,"IME-VERSION")) {
  418.                 /* tie off at end of phrase */
  419.       if (t = rfc822_parse_phrase (d)) *t = '\0';
  420.                 /* known version? */
  421.       if (strcmp (d,"1.0") && strcmp (d,"RFC-XXXX"))
  422.         mm_log ("Warning: message has unknown MIME version",PARSE);
  423.       MIMEp = T;        /* note that we are MIME */
  424.     }
  425.     break;
  426.       case 'N':            /* possible Newsgroups: */
  427.     if (!env->newsgroups && !strcmp (tmp+1,"EWSGROUPS")) {
  428.       t = env->newsgroups = (char *) fs_get (1 + strlen (d));
  429.       while (c = *d++) if (c != ' ' && c != '\t') *t++ = c;
  430.       *t++ = '\0';
  431.     }
  432.     break;
  433.       case 'R':            /* possible Reply-To: */
  434.     if (!strcmp (tmp+1,"EPLY-TO"))
  435.       rfc822_parse_adrlist (&env->reply_to,d,host);
  436.     break;
  437.       case 'S':            /* possible Subject: or Sender: */
  438.     if (!env->subject && !strcmp (tmp+1,"UBJECT"))
  439.       env->subject = cpystr (d);
  440.     else if (!strcmp (tmp+1,"ENDER"))
  441.       rfc822_parse_adrlist (&env->sender,d,host);
  442.     break;
  443.       case 'T':            /* possible To: */
  444.     if (!strcmp (tmp+1,"O")) rfc822_parse_adrlist (&env->to,d,host);
  445.     break;
  446.       default:
  447.     break;
  448.       }
  449.     }
  450.   }
  451.                 /* default Sender: and Reply-To: to From: */
  452.   if (!env->sender) env->sender = rfc822_cpy_adr (env->from);
  453.   if (!env->reply_to) env->reply_to = rfc822_cpy_adr (env->from);
  454.                 /* now parse the body */
  455.   if (body) rfc822_parse_content (body,bs,host,tmp);
  456. }
  457.  
  458. /* Parse a message body content
  459.  * Accepts: pointer to body structure
  460.  *        pointer to body
  461.  *        body byte count
  462.  *        pointer to local host name
  463.  *        pointer to scratch buffer
  464.  */
  465.  
  466. void rfc822_parse_content (BODY *body,STRING *bs,char *h,char *t)
  467. {
  468.   char c,c1,*s,*s1;
  469.   unsigned long pos = GETPOS (bs);
  470.   unsigned long i = SIZE (bs);
  471.   unsigned long j,k,m = 0;
  472.   PARAMETER *param;
  473.   PART *part = NIL;
  474.   body->size.ibytes = i;    /* note body size in all cases */
  475.   body->size.bytes = (body->encoding == ENCBASE64 ||
  476.               body->encoding == ENCBINARY) ? i : strcrlflen (bs);
  477.   switch (body->type) {        /* see if anything else special to do */
  478.   case TYPETEXT:        /* text content */
  479.     if (!body->subtype)        /* default subtype */
  480.       body->subtype = cpystr (rfc822_default_subtype (body->type));
  481.     if (!body->parameter) {    /* default parameters */
  482.       body->parameter = mail_newbody_parameter ();
  483.       body->parameter->attribute = cpystr ("charset");
  484.       body->parameter->value = cpystr ("US-ASCII");
  485.     }
  486.                 /* count number of lines */
  487.     while (i--) if ((SNX (bs)) == '\n') body->size.lines++;
  488.     break;
  489.  
  490.   case TYPEMESSAGE:        /* encapsulated message */
  491.     body->contents.msg.env = NIL;
  492.     body->contents.msg.body = NIL;
  493.     body->contents.msg.text = NIL;
  494.     body->contents.msg.offset = pos;
  495.                 /* encapsulated RFC-822 message? */
  496.     if (!strcmp (body->subtype,"RFC822")) {
  497.       if ((body->encoding == ENCBASE64) ||
  498.       (body->encoding == ENCQUOTEDPRINTABLE)
  499.       || (body->encoding == ENCOTHER)) {
  500.     mm_log ("Nested encoding of message contents",PARSE);
  501.     return;
  502.       }
  503.                 /* hunt for blank line */
  504.       for (c = '\012',j = 0; (i > j) && ((c != '\012') || (CHR(bs) != '\012'));
  505.        SNX (bs),j++) if (CHR (bs) != '\015') c = CHR (bs);
  506.       if (i > j) SNX (bs);    /* unless no more text, body starts here */
  507.                 /* note body text offset and header size */
  508.       j = (body->contents.msg.offset = GETPOS (bs)) - pos;
  509.       SETPOS (bs,pos);        /* copy header string */
  510.       s = (char *) fs_get (j + 1);
  511.       for (s1 = s,k = j; k--; *s1++ = SNX (bs));
  512.       s[j] = '\0';        /* tie off string (not really necessary) */
  513.                 /* now parse the body */
  514.       rfc822_parse_msg (&body->contents.msg.env,&body->contents.msg.body,s,j,
  515.             bs,h,t);
  516.       fs_give ((void **) &s);    /* free header string */
  517.       SETPOS (bs,pos);        /* restore position */
  518.     }
  519.                 /* count number of lines */
  520.     while (i--) if (SNX (bs) == '\n') body->size.lines++;
  521.     break;
  522.   case TYPEMULTIPART:        /* multiple parts */
  523.     if ((body->encoding == ENCBASE64) || (body->encoding == ENCQUOTEDPRINTABLE)
  524.     || (body->encoding == ENCOTHER)) {
  525.       mm_log ("Nested encoding of multipart contents",PARSE);
  526.       return;
  527.     }                /* find cookie */
  528.     for (*t = '\0',param = body->parameter; param && !*t; param = param->next)
  529.       if (!strcmp (param->attribute,"BOUNDARY")) strcpy (t,param->value);
  530.     if (!*t) strcpy (t,"-");    /* yucky default */
  531.     j = strlen (t);        /* length of cookie and header */
  532.     c = '\012';            /* initially at beginning of line */
  533.  
  534.     while (i > j) switch (c) {    /* examine each line */
  535.     case '\015':        /* handle CRLF form */
  536.       if (CHR (bs) == '\012') {    /* following LF? */
  537.     c = SNX (bs); i--;    /* yes, slurp it */
  538.       }
  539.     case '\012':        /* at start of a line, start with -- ? */
  540.       m = GETPOS (bs);        /* note start of line */
  541.       if (i-- && ((c = SNX (bs)) == '-') && i-- && ((c = SNX (bs)) == '-')) {
  542.                 /* see if cookie matches */
  543.     for (k = j,s = t; i-- && *s++ == (c = SNX (bs)) && --k;);
  544.     if (k) break;        /* strings didn't match if non-zero */
  545.                 /* look at what follows cookie */
  546.     if (i && i--) switch (c = SNX (bs)) {
  547.     case '-':        /* at end if two dashes */
  548.       if ((i && i--) && ((c = SNX (bs)) == '-') &&
  549.           ((i && i--) ? (((c = SNX (bs)) == '\015') || (c=='\012')) : T)) {
  550.                 /* if have a final part calculate its size */
  551.         if (part && (part->body.size.bytes = m - part->offset))
  552.           part->body.size.bytes--;
  553.         part = NIL; i = 1;    /* terminate scan */
  554.       }
  555.       break;
  556.     case '\015':        /* handle CRLF form */
  557.       if (i && CHR (bs) == '\012') {
  558.         c = SNX (bs); i--;    /* yes, slurp it */
  559.       }
  560.     case '\012':        /* new line */
  561.       if (part) {        /* calculate size of previous */
  562.         part->body.size.bytes = m - part->offset;
  563.                 /* instantiate next */
  564.         part = part->next = mail_newbody_part ();
  565.       }            /* otherwise start new list */
  566.       else part = body->contents.part = mail_newbody_part ();
  567.                 /* note offset from main body */
  568.       part->offset = GETPOS (bs);
  569.       break;
  570.     default:        /* whatever it was it wasn't valid */
  571.       break;
  572.     }
  573.       }
  574.       break;
  575.     default:            /* not at a line */
  576.       c = SNX (bs); i--;    /* get next character */
  577.       break;
  578.     }                /* calculate size of any final part */
  579.     if (part) part->body.size.bytes = GETPOS (bs) - part->offset;
  580.  
  581.                 /* parse body parts */
  582.     for (part = body->contents.part; part; part = part->next) {
  583.                 /* get size of this part, ignore if empty */
  584.       if (i = part->body.size.bytes) {
  585.     SETPOS (bs,part->offset);
  586.                 /* until end of header */
  587.     while (i > 0 && (CHR (bs) != '\012')) {
  588.       s1 = t;        /* initialize buffer pointer */
  589.       c = ' ';        /* and previous character */
  590.       while (c) {        /* collect text until logical end of line */
  591.         switch (c1 = SNX (bs)) {
  592.         case '\012':    /* newline, possible end of logical line */
  593.           if ((i > 0) && (CHR (bs) == '\015')) {
  594.         SNX (bs); i--;    /* eat any CR following */
  595.           }
  596.                 /* tie off unless next line starts with WS */
  597.           if (!i || ((CHR (bs) != ' ') && (CHR(bs) != '\t')))
  598.         *s1 = c = '\0';
  599.           break;
  600.         case '\015':    /* return */
  601.         case '\t':        /* tab */
  602.         case ' ':        /* insert whitespace if not already there */
  603.           if (c != ' ') *s1++ = c = ' ';
  604.           break;
  605.         default:        /* all other characters */
  606.           *s1++ = c = c1;    /* insert the character into the line */
  607.           break;
  608.         }
  609.                 /* end of data ties off the header */
  610.         if (!--i) *s1++ = c = '\0';
  611.       }
  612.                 /* find header item type */
  613.       if (s = strchr (t,':')) {
  614.         *s++ = '\0';    /* tie off header item, point at its data */
  615.                 /* flush whitespace */
  616.         while (*s == ' ') s++;
  617.         if (s1 = strchr (ucase (t),' ')) *s1 = '\0';
  618.         if ((t[0] == 'C') && (t[1] == 'O') && (t[2] == 'N') &&
  619.         (t[3] == 'T') && (t[4] == 'E') && (t[5] == 'N') &&
  620.         (t[6] == 'T') && (t[7] == '-'))
  621.           rfc822_parse_content_header (&part->body,t+8,s);
  622.       }
  623.     }            /* skip trailing (CR)LF */
  624.     if ((i > 0) && (CHR (bs) =='\015')) {i--; SNX (bs);}
  625.     if ((i > 0) && (CHR (bs) =='\012')) {i--; SNX (bs);}
  626.     j = bs->size;        /* save upper level size */
  627.                 /* set size and offset for next level */
  628.     bs->size = (part->offset = GETPOS (bs)) + i;
  629.                 /* now parse it */
  630.     rfc822_parse_content (&part->body,bs,h,t);
  631.     bs->size = j;        /* restore current level size */
  632.       }
  633.  
  634.       else {
  635.     if (!body->subtype)    /* default subtype */
  636.       body->subtype = cpystr (rfc822_default_subtype (body->type));
  637.     if (!body->parameter) {    /* default parameters */
  638.       body->parameter = mail_newbody_parameter ();
  639.       body->parameter->attribute = cpystr ("charset");
  640.       body->parameter->value = cpystr ("US-ASCII");
  641.     }
  642.       }
  643.     }
  644.     break;
  645.   default:            /* nothing special to do in any other case */
  646.     break;
  647.   }
  648. }
  649.  
  650. /* Parse RFC822 body content header
  651.  * Accepts: body to write to
  652.  *        possible content name
  653.  *        remainder of header
  654.  */
  655.  
  656. void rfc822_parse_content_header (BODY *body,char *name,char *s)
  657. {
  658.   PARAMETER *param = NIL;
  659.   char tmp[MAILTMPLEN];
  660.   char c,*t;
  661.   long i;
  662.   switch (*name) {
  663.   case 'I':            /* possible Content-ID */
  664.     if (!(strcmp (name+1,"D") || body->id)) body->id = cpystr (s);
  665.     break;
  666.   case 'D':            /* possible Content-Description */
  667.     if (!(strcmp (name+1,"ESCRIPTION")) || body->description)
  668.       body->description = cpystr (s);
  669.     break;
  670.   case 'T':            /* possible Content-Type/Transfer-Encoding */
  671.     if (!(strcmp (name+1,"YPE") || body->type || body->subtype ||
  672.       body->parameter)) {
  673.                 /* get type word */
  674.       if (!(name = rfc822_parse_word (s,ptspecials))) break;
  675.       c = *name;        /* remember delimiter */
  676.       *name = '\0';        /* tie off type */
  677.       ucase (s);        /* search for body type */
  678.       for (i = 0; (i < TYPEOTHER) && strcmp (s,body_types[i]); i++);
  679.       body->type = i;        /* set body type */
  680.       *name = c;        /* restore delimiter */
  681.       rfc822_skipws (&name);    /* skip whitespace */
  682.       if ((*name == '/') &&    /* subtype? */
  683.       (name = rfc822_parse_word ((s = ++name),ptspecials))) {
  684.     c = *name;        /* save delimiter */
  685.     *name = '\0';        /* tie off subtype */
  686.     rfc822_skipws (&s);    /* copy subtype */
  687.     body->subtype = ucase (cpystr (s ? s :
  688.                        rfc822_default_subtype (body->type)));
  689.     *name = c;        /* restore delimiter */
  690.     rfc822_skipws (&name);    /* skip whitespace */
  691.       }
  692.                 /* subtype defaulting is a no-no, but... */
  693.       else {
  694.     body->subtype = cpystr (rfc822_default_subtype (body->type));
  695.     if (!name) {        /* did the fool have a subtype delimiter? */
  696.       name = s;        /* barf, restore pointer */
  697.       rfc822_skipws (&name);/* skip leading whitespace */
  698.     }
  699.       }
  700.  
  701.                 /* parameter list? */
  702.       while (name && (*name == ';') &&
  703.          (name = rfc822_parse_word ((s = ++name),ptspecials))) {
  704.     c = *name;        /* remember delimiter */
  705.     *name = '\0';        /* tie off attribute name */
  706.     rfc822_skipws (&s);    /* skip leading attribute whitespace */
  707.     if (!*s) *name = c;    /* must have an attribute name */
  708.     else {            /* instantiate a new parameter */
  709.       if (body->parameter) param = param->next = mail_newbody_parameter ();
  710.       else param = body->parameter = mail_newbody_parameter ();
  711.       param->attribute = ucase (cpystr (s));
  712.       *name = c;        /* restore delimiter */
  713.       rfc822_skipws (&name);/* skip whitespace before equal sign */
  714.       if ((*name != '=') ||    /* missing value is a no-no too */
  715.           !(name = rfc822_parse_word ((s = ++name),ptspecials)))
  716.         param->value = cpystr ("UNKNOWN");
  717.       else {        /* good, have equals sign */
  718.         c = *name;        /* remember delimiter */
  719.         *name = '\0';    /* tie off value */
  720.         rfc822_skipws (&s);    /* skip leading value whitespace */
  721.         if (*s) param->value = rfc822_cpy (s);
  722.         *name = c;        /* restore delimiter */
  723.         rfc822_skipws (&name);
  724.       }
  725.     }
  726.       }
  727.       if (!name) {        /* must be end of poop */
  728.     if (param && param->attribute)
  729.       sprintf (tmp,"Missing parameter value: %.80s",param->attribute);
  730.     else strcpy (tmp,"Missing parameter");
  731.     mm_log (tmp,PARSE);
  732.       }
  733.       else if (*name) {        /* must be end of poop */
  734.     sprintf (tmp,"Junk at end of parameters: %.80s",name);
  735.     mm_log (tmp,PARSE);
  736.       }
  737.     }
  738.     else if (!strcmp (name+1,"RANSFER-ENCODING")) {
  739.                 /* flush out any confusing whitespace */
  740.       if (t = strchr (ucase (s),' ')) *t = '\0';
  741.                 /* search for body encoding */
  742.       for (i = 0; (i < ENCOTHER) && strcmp (s,body_encodings[i]); i++);
  743.       body->encoding = i;    /* set body type */
  744.     }
  745.     break;
  746.   default:            /* otherwise unknown */
  747.     break;
  748.   }
  749. }
  750.  
  751. /* Parse RFC822 address list
  752.  * Accepts: address list to write to
  753.  *        input string
  754.  *        default host name
  755.  */
  756.  
  757. void rfc822_parse_adrlist (ADDRESS **lst,char *string,char *host)
  758. {
  759.   char tmp[MAILTMPLEN];
  760.   char *p,*s;
  761.   long n = 0;
  762.   ADDRESS *last = *lst;
  763.   ADDRESS *adr;
  764.                 /* run to tail of list */
  765.   if (last) while (last->next) last = last->next;
  766.   rfc822_skipws (&string);    /* skip leading WS */
  767.                 /* loop until string exhausted */
  768.   if (*string != '\0') while (p = string) {
  769.                 /* see if start of group */
  770.     while ((*p == ':') || (p = rfc822_parse_phrase (string))) {
  771.       s = p;            /* end of phrase */
  772.       rfc822_skipws (&s);    /* find delimiter */
  773.       if (*s == ':') {        /* really a group? */
  774.     n++;            /* another level */
  775.     *p++ = '\0';        /* tie off group name */
  776.     rfc822_skipws (&p);    /* skip subsequent whitespace */
  777.                 /* write as address */
  778.     (adr = mail_newaddr ())->mailbox = rfc822_cpy (string);
  779.     if (!*lst) *lst = adr;    /* first time through? */
  780.     else last->next = adr;    /* no, append to the list */
  781.     last = adr;        /* set for subsequent linking */
  782.     string = p;        /* continue after this point */
  783.       }
  784.       else break;        /* bust out of this */
  785.     }
  786.     rfc822_skipws (&string);    /* skip any following whitespace */
  787.     if (!string) break;        /* punt if unterminated group */
  788.                 /* if not empty group */
  789.     if (*string != ';' || n <= 0) {
  790.                 /* got an address? */
  791.       if (adr = rfc822_parse_address (&string,host)) {
  792.     if (!*lst) *lst = adr;    /* yes, first time through? */
  793.     else last->next = adr;    /* no, append to the list */
  794.     last = adr;        /* set for subsequent linking */
  795.       }
  796.       else if (string) {    /* bad mailbox */
  797.     sprintf (tmp,"Bad mailbox: %.80s",string);
  798.     mm_log (tmp,PARSE);
  799.     break;
  800.       }
  801.     }
  802.  
  803.                 /* handle end of group */
  804.     if (string && *string == ';' && n >= 0) {
  805.       n--;            /* out of this group */
  806.       string++;            /* skip past the semicolon */
  807.                 /* append end of address mark to the list */
  808.       last->next = (adr = mail_newaddr ());
  809.       last = adr;        /* set for subsequent linking */
  810.       rfc822_skipws (&string);    /* skip any following whitespace */
  811.       switch (*string) {    /* see what follows */
  812.       case ',':            /* another address? */
  813.     ++string;        /* yes, skip past the comma */
  814.       case ';':            /* another end of group? */
  815.       case '\0':        /* end of string */
  816.     break;
  817.       default:
  818.     sprintf (tmp,"Junk at end of group: %.80s",string);
  819.     mm_log (tmp,PARSE);
  820.     break;
  821.       }
  822.     }
  823.   }
  824.   while (n-- > 0) {        /* if unterminated groups */
  825.     last->next = (adr = mail_newaddr ());
  826.     last = adr;            /* set for subsequent linking */
  827.   }
  828. }
  829.  
  830. /* Parse RFC822 address
  831.  * Accepts: pointer to string pointer
  832.  *        default host
  833.  * Returns: address
  834.  *
  835.  * Updates string pointer
  836.  */
  837.  
  838. ADDRESS *rfc822_parse_address (char **string,char *defaulthost)
  839. {
  840.   char tmp[MAILTMPLEN];
  841.   ADDRESS *adr;
  842.   char c,*s;
  843.   char *phrase;
  844.   if (!string) return NIL;
  845.   rfc822_skipws (string);    /* flush leading whitespace */
  846.  
  847.   /* This is much more complicated than it should be because users like
  848.    * to write local addrspecs without "@localhost".  This makes it very
  849.    * difficult to tell a phrase from an addrspec!
  850.    * The other problem we must cope with is a route-addr without a leading
  851.    * phrase.  Yuck!
  852.    */
  853.  
  854.   if (*(s = *string) == '<')     /* note start, handle case of phraseless RA */
  855.     adr = rfc822_parse_routeaddr (s,string,defaulthost);
  856.   else {            /* get phrase if any */
  857.     if ((phrase = rfc822_parse_phrase (s)) &&
  858.     (adr = rfc822_parse_routeaddr (phrase,string,defaulthost))) {
  859.       *phrase = '\0';        /* tie off phrase */
  860.                 /* phrase is a personal name */
  861.       adr->personal = rfc822_cpy (s);
  862.     }
  863.     else adr = rfc822_parse_addrspec (s,string,defaulthost);
  864.   }
  865.                 /* analyze what follows */
  866.   if (*string) switch (c = **string) {
  867.   case ',':            /* comma? */
  868.     ++*string;            /* then another address follows */
  869.     break;
  870.   case ';':            /* possible end of group? */
  871.     break;            /* let upper level deal with it */
  872.   default:
  873.     s = isalnum (c) ? "Must use comma to separate addresses: %.80s" :
  874.       "Junk at end of address: %.80s";
  875.     sprintf (tmp,s,*string);
  876.     mm_log (tmp,PARSE);
  877.                 /* falls through */
  878.   case '\0':            /* null-specified address? */
  879.     *string = NIL;        /* punt remainder of parse */
  880.     break;
  881.   }
  882.   return adr;            /* return the address */
  883. }
  884.  
  885. /* Parse RFC822 route-address
  886.  * Accepts: string pointer
  887.  *        pointer to string pointer to update
  888.  * Returns: address
  889.  *
  890.  * Updates string pointer
  891.  */
  892.  
  893. ADDRESS *rfc822_parse_routeaddr (char *string,char **ret,char *defaulthost)
  894. {
  895.   char tmp[MAILTMPLEN];
  896.   ADDRESS *adr;
  897.   char *adl = NIL;
  898.   char *routeend = NIL;
  899.   if (!string) return NIL;
  900.   rfc822_skipws (&string);    /* flush leading whitespace */
  901.                 /* must start with open broket */
  902.   if (*string != '<') return NIL;
  903.   if (string[1] == '@') {    /* have an A-D-L? */
  904.     adl = ++string;        /* yes, remember that fact */
  905.     while (*string != ':') {    /* search for end of A-D-L */
  906.                 /* punt if never found */
  907.       if (*string == '\0') return NIL;
  908.       ++string;            /* try next character */
  909.     }
  910.     *string = '\0';        /* tie off A-D-L */
  911.     routeend = string;        /* remember in case need to put back */
  912.   }
  913.                 /* parse address spec */
  914.   if (!(adr = rfc822_parse_addrspec (++string,ret,defaulthost))) {
  915.     if (adl) *routeend = ':';    /* put colon back since parse barfed */
  916.     return NIL;
  917.   }
  918.                 /* have an A-D-L? */
  919.   if (adl) adr->adl = cpystr (adl);
  920.                 /* make sure terminated OK */
  921.     if (*ret) if (**ret == '>') {
  922.     ++*ret;            /* skip past the broket */
  923.     rfc822_skipws (ret);    /* flush trailing WS */
  924.                 /* wipe pointer if at end of string */
  925.     if (**ret == '\0') *ret = NIL;
  926.     return adr;            /* return the address */
  927.   }
  928.   sprintf (tmp,"Unterminated mailbox: %.80s@%.80s",adr->mailbox,adr->host);
  929.   mm_log (tmp,PARSE);
  930.   return adr;            /* return the address */
  931. }
  932.  
  933. /* Parse RFC822 address-spec
  934.  * Accepts: string pointer
  935.  *        pointer to string pointer to update
  936.  *        default host
  937.  * Returns: address
  938.  *
  939.  * Updates string pointer
  940.  */
  941.  
  942. ADDRESS *rfc822_parse_addrspec (char *string,char **ret,char *defaulthost)
  943. {
  944.   ADDRESS *adr;
  945.   char *end;
  946.   char c,*s,*t;
  947.   if (!string) return NIL;
  948.   rfc822_skipws (&string);    /* flush leading whitespace */
  949.                 /* find end of mailbox */
  950.   if (!(end = rfc822_parse_word (string,NIL))) return NIL;
  951.   adr = mail_newaddr ();    /* create address block */
  952.   c = *end;            /* remember delimiter */
  953.   *end = '\0';            /* tie off mailbox */
  954.                 /* copy mailbox */
  955.   adr->mailbox = rfc822_cpy (string);
  956.   *end = c;            /* restore delimiter */
  957.   t = end;            /* remember end of mailbox for no host case */
  958.   rfc822_skipws (&end);        /* skip whitespace */
  959.   if (*end == '@') {        /* have host name? */
  960.     ++end;            /* skip delimiter */
  961.     rfc822_skipws (&end);    /* skip whitespace */
  962.     *ret = end;            /* update return pointer */
  963.                     /* search for end of host */
  964.     if (end = rfc822_parse_word ((string = end),hspecials)) {
  965.       c = *end;            /* remember delimiter */
  966.       *end = '\0';        /* tie off host */
  967.                 /* copy host */
  968.       adr->host = rfc822_cpy (string);
  969.       *end = c;            /* restore delimiter */
  970.     }
  971.     else mm_log ("Missing or invalid host name after @",PARSE);
  972.   }
  973.   else end = t;            /* make person name default start after mbx */
  974.  
  975.                 /* default host if missing */
  976.   if (!adr->host) adr->host = cpystr (defaulthost);
  977.   if (end && !adr->personal) {    /* try person name in comments if missing */
  978.     while (*end == ' ') ++end;    /* see if we can find a person name here */
  979.                 /* found something that may be a name? */
  980.     if ((*end == '(') && (t = s = rfc822_parse_phrase (end + 1))) {
  981.       rfc822_skipws (&s);    /* heinous kludge for trailing WS in comment */
  982.       if (*s == ')') {        /* look like good end of comment? */
  983.     *t = '\0';        /* yes, tie off the likely name and copy */
  984.     ++end;            /* skip over open paren now */
  985.     rfc822_skipws (&end);
  986.     adr->personal = rfc822_cpy (end);
  987.     end = ++s;        /* skip after it */
  988.       }
  989.     }
  990.     rfc822_skipws (&end);    /* skip any other WS in the normal way */
  991.   }
  992.                 /* set return to end pointer */
  993.   *ret = (end && *end) ? end : NIL;
  994.   return adr;            /* return the address we got */
  995. }
  996.  
  997. /* Parse RFC822 phrase
  998.  * Accepts: string pointer
  999.  * Returns: pointer to end of phrase
  1000.  */
  1001.  
  1002. char *rfc822_parse_phrase (char *s)
  1003. {
  1004.   char *curpos;
  1005.   if (!s) return NIL;        /* no-op if no string */
  1006.                 /* find first word of phrase */
  1007.   curpos = rfc822_parse_word (s,NIL);
  1008.   if (!curpos) return NIL;    /* no words means no phrase */
  1009.                 /* check if string ends with word */
  1010.   if (*curpos == '\0') return curpos;
  1011.   s = curpos;            /* sniff past the end of this word and WS */
  1012.   rfc822_skipws (&s);        /* skip whitespace */
  1013.                 /* recurse to see if any more */
  1014.   return (s = rfc822_parse_phrase (s)) ? s : curpos;
  1015. }
  1016.  
  1017. /* Parse RFC822 word
  1018.  * Accepts: string pointer
  1019.  * Returns: pointer to end of word
  1020.  */
  1021.  
  1022. char *rfc822_parse_word (char *s,const char *delimiters)
  1023. {
  1024.   char *st,*str;
  1025.   if (!s) return NIL;        /* no-op if no string */
  1026.   rfc822_skipws (&s);        /* flush leading whitespace */
  1027.   if (*s == '\0') return NIL;    /* end of string */
  1028.                 /* default delimiters to standard */
  1029.   if (!delimiters) delimiters = wspecials;
  1030.   str = s;            /* hunt pointer for strpbrk */
  1031.   while (T) {            /* look for delimiter */
  1032.     if (!(st = strpbrk (str,delimiters))) {
  1033.       while (*s != '\0') ++s;    /* no delimiter, hunt for end */
  1034.       return s;            /* return it */
  1035.     }
  1036.     switch (*st) {        /* dispatch based on delimiter */
  1037.     case '"':            /* quoted string */
  1038.                 /* look for close quote */
  1039.       while (*++st != '"') switch (*st) {
  1040.       case '\0':        /* unbalanced quoted string */
  1041.     return NIL;        /* sick sick sick */
  1042.       case '\\':        /* quoted character */
  1043.     ++st;            /* skip the next character */
  1044.       default:            /* ordinary character */
  1045.     break;            /* no special action */
  1046.       }
  1047.       str = ++st;        /* continue parse */
  1048.       break;
  1049.     case '\\':            /* quoted character */
  1050.       str = st + 2;        /* skip quoted character and go on */
  1051.       break;
  1052.     default:            /* found a word delimiter */
  1053.       return (st == s) ? NIL : st;
  1054.     }
  1055.   }
  1056. }
  1057.  
  1058. /* Copy an RFC822 format string
  1059.  * Accepts: string
  1060.  * Returns: copy of string
  1061.  */
  1062.  
  1063. char *rfc822_cpy (char *src)
  1064. {
  1065.                 /* copy and unquote */
  1066.   return rfc822_quote (cpystr (src));
  1067. }
  1068.  
  1069.  
  1070. /* Unquote an RFC822 format string
  1071.  * Accepts: string
  1072.  * Returns: string
  1073.  */
  1074.  
  1075. char *rfc822_quote (char *src)
  1076. {
  1077.   char *ret = src;
  1078.   if (strpbrk (src,"\\\"")) {    /* any quoting in string? */
  1079.     char *dst = ret;
  1080.     while (*src) {        /* copy string */
  1081.       if (*src == '\"') src++;    /* skip double quote entirely */
  1082.       else {
  1083.     if (*src == '\\') src++;/* skip over single quote, copy next always */
  1084.     *dst++ = *src++;    /* copy character */
  1085.       }
  1086.     }
  1087.     *dst = '\0';        /* tie off string */
  1088.   }
  1089.   return ret;            /* return our string */
  1090. }
  1091.  
  1092.  
  1093. /* Copy address list
  1094.  * Accepts: address list
  1095.  * Returns: address list
  1096.  */
  1097.  
  1098. ADDRESS *rfc822_cpy_adr (ADDRESS *adr)
  1099. {
  1100.   ADDRESS *dadr;
  1101.   ADDRESS *ret = NIL;
  1102.   ADDRESS *prev = NIL;
  1103.   while (adr) {            /* loop while there's still an MAP adr */
  1104.     dadr = mail_newaddr ();    /* instantiate a new address */
  1105.     if (!ret) ret = dadr;    /* note return */
  1106.     if (prev) prev->next = dadr;/* tie on to the end of any previous */
  1107.     dadr->personal = cpystr (adr->personal);
  1108.     dadr->adl = cpystr (adr->adl);
  1109.     dadr->mailbox = cpystr (adr->mailbox);
  1110.     dadr->host = cpystr (adr->host);
  1111.     prev = dadr;        /* this is now the previous */
  1112.     adr = adr->next;        /* go to next address in list */
  1113.   }
  1114.   return (ret);            /* return the MTP address list */
  1115. }
  1116.  
  1117. /* Skips RFC822 whitespace
  1118.  * Accepts: pointer to string pointer
  1119.  */
  1120.  
  1121. void rfc822_skipws (char **s)
  1122. {
  1123.   char tmp[MAILTMPLEN];
  1124.   char *t;
  1125.   long n = 0;
  1126.                 /* while whitespace or start of comment */
  1127.   while ((**s == ' ') || (n = (**s == '('))) {
  1128.     t = *s;            /* note comment pointer */
  1129.     if (**s == '(') while (n) {    /* while comment in effect */
  1130.       switch (*++*s) {        /* get character of comment */
  1131.       case '(':            /* nested comment? */
  1132.     n++;            /* increment nest count */
  1133.     break;
  1134.       case ')':            /* end of comment? */
  1135.     n--;            /* decrement nest count */
  1136.     break;
  1137.       case '"':            /* quoted string? */
  1138.     while (*++*s != '\"') if (!**s || (**s == '\\' && !*++*s)) {
  1139.       sprintf (tmp,"Unterminated quoted string within comment: %.80s",t);
  1140.       mm_log (tmp,PARSE);
  1141.       *t = '\0';        /* nuke duplicate messages in case reparse */
  1142.       return;
  1143.     }
  1144.     break;
  1145.       case '\\':        /* quote next character? */
  1146.     if (*++*s != '\0') break;
  1147.       case '\0':        /* end of string */
  1148.     sprintf (tmp,"Unterminated comment: %.80s",t);
  1149.     mm_log (tmp,PARSE);
  1150.     *t = '\0';        /* nuke duplicate messages in case reparse */
  1151.     return;            /* this is wierd if it happens */
  1152.       default:            /* random character */
  1153.     break;
  1154.       }
  1155.     }
  1156.     ++*s;            /* skip past whitespace character */
  1157.   }
  1158. }
  1159.  
  1160. /* Body contents utility and encoding/decoding routines */
  1161.  
  1162.  
  1163. /* Return body contents in normal form
  1164.  * Accepts: pointer to destination
  1165.  *        pointer to length of destination
  1166.  *        returned destination length
  1167.  *        source
  1168.  *        length of source
  1169.  *        source encoding
  1170.  * Returns: destination
  1171.  *
  1172.  * Originally, this routine was supposed to do decoding as well, but that was
  1173.  * moved to a higher level.  Now, it's merely a jacket into strcrlfcpy that
  1174.  * avoids the work for BASE64 and BINARY segments.
  1175.  */
  1176.  
  1177. char *rfc822_contents (char **dst,unsigned long *dstl,unsigned long *len,
  1178.                char *src,unsigned long srcl,unsigned short encoding)
  1179. {
  1180.   *len = 0;            /* in case we return an error */
  1181.   switch (encoding) {        /* act based on encoding */
  1182.   case ENCBASE64:        /* 3 to 4 binary */
  1183.   case ENCBINARY:        /* unmodified binary */
  1184.     if ((*len = srcl) > *dstl) {/* resize if not enough space */
  1185.       fs_give ((void **) dst);    /* fs_resize does an unnecessary copy */
  1186.       *dst = (char *) fs_get ((*dstl = srcl) + 1);
  1187.     }
  1188.     memcpy (*dst,src,srcl);    /* copy that many bytes */
  1189.     *(*dst + srcl) = '\0';    /* tie off destination */
  1190.     break;
  1191.   default:            /* all other cases return strcrlfcpy version */
  1192.     *len = strlen (*dst = strcrlfcpy (dst,dstl,src,srcl));
  1193.     break;
  1194.   }
  1195.   return *dst;            /* return the string */
  1196. }
  1197.  
  1198.  
  1199. /* Output RFC 822 message
  1200.  * Accepts: temporary buffer
  1201.  *        envelope
  1202.  *        body
  1203.  *        I/O routine
  1204.  *        stream for I/O routine
  1205.  * Returns: T if successful, NIL if failure
  1206.  */
  1207.  
  1208. long rfc822_output (char *t,ENVELOPE *env,BODY *body,soutr_t f,TCPSTREAM *s)
  1209. {
  1210.   rfc822_encode_body (env,body);/* encode body as necessary */
  1211.   rfc822_header (t,env,body);    /* build RFC822 header */
  1212.                 /* output header and body */
  1213.   return (*f) (s,t) && (body ? rfc822_output_body (body,f,s) : T);
  1214. }
  1215.  
  1216. /* Encode a body for 7BIT transmittal
  1217.  * Accepts: envelope
  1218.  *        body
  1219.  */
  1220.  
  1221. void rfc822_encode_body (ENVELOPE *env,BODY *body)
  1222. {
  1223.   void *f;
  1224.   PART *part;
  1225.   if (body) switch (body->type) {
  1226.   case TYPEMULTIPART:        /* multi-part */
  1227.     if (!body->parameter) {    /* cookie not set up yet? */
  1228.       char tmp[MAILTMPLEN];    /* make cookie not in BASE64 or QUOTEPRINT*/
  1229.       sprintf (tmp,"%ld-%ld-%ld:#%ld",gethostid (),random (),time (0),
  1230.            getpid ());
  1231.       body->parameter = mail_newbody_parameter ();
  1232.       body->parameter->attribute = cpystr ("BOUNDARY");
  1233.       body->parameter->value = cpystr (tmp);
  1234.     }
  1235.     part = body->contents.part;    /* encode body parts */
  1236.     do rfc822_encode_body (env,&part->body);
  1237.     while (part = part->next);    /* until done */
  1238.     break;
  1239.   case TYPEMESSAGE:        /* encapsulated message */
  1240.     if (!((body->encoding == ENC7BIT) || (body->encoding == ENC8BIT) ||
  1241.       (body->encoding == ENCBINARY)))
  1242.       fatal ("Invalid rfc822_encode_body message encoding");
  1243.     break;            /* can't change encoding */
  1244.   default:            /* all else has some encoding */
  1245.     switch (body->encoding) {
  1246.     case ENC8BIT:        /* encode 8BIT into QUOTED-PRINTABLE */
  1247.                 /* remember old 8-bit contents */
  1248.       f = (void *) body->contents.text;
  1249.       body->contents.text = rfc822_8bit (body->contents.text,body->size.bytes,
  1250.                      &body->size.bytes);
  1251.       body->encoding = ENCQUOTEDPRINTABLE;
  1252.       fs_give (&f);        /* flush old binary contents */
  1253.       break;
  1254.     case ENCBINARY:        /* encode binary into BASE64 */
  1255.       f = body->contents.binary;/* remember old binary contents */
  1256.       body->contents.text = rfc822_binary (body->contents.binary,
  1257.                        body->size.bytes,&body->size.bytes);
  1258.       body->encoding = ENCBASE64;
  1259.       fs_give (&f);        /* flush old binary contents */
  1260.     default:            /* otherwise OK */
  1261.       break;
  1262.     }
  1263.     break;
  1264.   }
  1265. }
  1266.  
  1267. /* Output RFC 822 body
  1268.  * Accepts: body
  1269.  *        I/O routine
  1270.  *        stream for I/O routine
  1271.  * Returns: T if successful, NIL if failure
  1272.  */
  1273.  
  1274. long rfc822_output_body (BODY *body,soutr_t f,TCPSTREAM *s)
  1275. {
  1276.   PART *part;
  1277.   PARAMETER *param;
  1278.   char *cookie = NIL;
  1279.   char tmp[MAILTMPLEN];
  1280.   char *t;
  1281.   switch (body->type) {
  1282.   case TYPEMULTIPART:        /* multipart gets special handling */
  1283.     part = body->contents.part;    /* first body part */
  1284.                 /* find cookie */
  1285.     for (param = body->parameter; param && !cookie; param = param->next)
  1286.       if (!strcmp (param->attribute,"BOUNDARY")) cookie = param->value;
  1287.     if (!cookie) cookie = "-";    /* yucky default */
  1288.     do {            /* for each part */
  1289.                 /* build cookie */
  1290.       sprintf (t = tmp,"--%s\015\012",cookie);
  1291.                 /* append mini-header */
  1292.       rfc822_write_body_header (&t,&part->body);
  1293.       strcat (t,"\015\012");    /* write terminating blank line */
  1294.                 /* output cookie, mini-header, and contents */
  1295.       if (!((*f) (s,tmp) && rfc822_output_body (&part->body,f,s))) return NIL;
  1296.     } while (part = part->next);/* until done */
  1297.                 /* output trailing cookie */
  1298.     sprintf (t = tmp,"--%s--",cookie);
  1299.     break;
  1300.   case TYPEMESSAGE:        /* encapsulated message */
  1301.     t = body->contents.msg.text;
  1302.     break;
  1303.   default:            /* all else is text now */
  1304.     t = (char *) body->contents.text;
  1305.     break;
  1306.   }
  1307.                 /* output final stuff */
  1308.   if (t && *t && !((*f) (s,t) && (*f) (s,"\015\012"))) return NIL;
  1309.   return LONGT;
  1310. }
  1311.  
  1312. /* Convert BASE64 contents to binary
  1313.  * Accepts: source
  1314.  *        length of source
  1315.  *        pointer to return destination length
  1316.  * Returns: destination as binary
  1317.  */
  1318.  
  1319. void *rfc822_base64 (unsigned char *src,unsigned long srcl,unsigned long *len)
  1320. {
  1321.   char c;
  1322.   void *ret = fs_get (4 + ((srcl * 3) / 4));
  1323.   char *d = (char *) ret;
  1324.   short e = 0;
  1325.   *len = 0;            /* in case we return an error */
  1326.   while (srcl--) {        /* until run out of characters */
  1327.     c = *src++;            /* simple-minded decode */
  1328.     if (isupper (c)) c -= 'A';
  1329.     else if (islower (c)) c -= 'a' - 26;
  1330.     else if (isdigit (c)) c -= '0' - 52;
  1331.     else if (c == '+') c = 62;
  1332.     else if (c == '/') c = 63;
  1333.     else if (c == '=') {    /* padding */
  1334.       switch (e++) {        /* check quantum position */
  1335.       case 2:
  1336.     if (*src != '=') return NIL;
  1337.     break;
  1338.       case 3:
  1339.     e = 0;            /* restart quantum */
  1340.     break;
  1341.       default:            /* impossible quantum position */
  1342.     fs_give (&ret);
  1343.     return NIL;
  1344.       }
  1345.       continue;
  1346.     }
  1347.     else continue;        /* junk character */
  1348.     switch (e++) {        /* install based on quantum position */
  1349.     case 0:
  1350.       *d = c << 2;        /* byte 1: high 6 bits */
  1351.       break;
  1352.     case 1:
  1353.       *d++ |= c >> 4;        /* byte 1: low 2 bits */
  1354.       *d = c << 4;        /* byte 2: high 4 bits */
  1355.       break;
  1356.     case 2:
  1357.       *d++ |= c >> 2;        /* byte 2: low 4 bits */
  1358.       *d = c << 6;        /* byte 3: high 2 bits */
  1359.       break;
  1360.     case 3:
  1361.       *d++ |= c;        /* byte 3: low 6 bits */
  1362.       e = 0;            /* reinitialize mechanism */
  1363.       break;
  1364.     }
  1365.   }
  1366.   *len = d - (char *) ret;    /* calculate data length */
  1367.   return ret;            /* return the string */
  1368. }
  1369.  
  1370. /* Convert binary contents to BASE64
  1371.  * Accepts: source
  1372.  *        length of source
  1373.  *        pointer to return destination length
  1374.  * Returns: destination as BASE64
  1375.  */
  1376.  
  1377. unsigned char *rfc822_binary (void *src,unsigned long srcl,unsigned long *len)
  1378. {
  1379.   unsigned char *ret,*d;
  1380.   unsigned char *s = (unsigned char *) src;
  1381.   char *v = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  1382.   unsigned long i = ((srcl + 2) / 3) * 4;
  1383.   *len = i += 2 * ((i / 60) + 1);
  1384.   d = ret = (unsigned char *) fs_get (++i);
  1385.   for (i = 0; srcl; s += 3) {    /* process tuplets */
  1386.     *d++ = v[s[0] >> 2];    /* byte 1: high 6 bits (1) */
  1387.                 /* byte 2: low 2 bits (1), high 4 bits (2) */
  1388.     *d++ = v[((s[0] << 4) + (--srcl ? (s[1] >> 4) : 0)) & 0x3f];
  1389.                 /* byte 3: low 4 bits (2), high 2 bits (3) */
  1390.     *d++ = srcl ? v[((s[1] << 2) + (--srcl ? (s[2] >> 6) : 0)) & 0x3f] : '=';
  1391.                 /* byte 4: low 6 bits (3) */
  1392.     *d++ = srcl ? v[s[2] & 0x3f] : '=';
  1393.     if (srcl) srcl--;        /* count third character if processed */
  1394.     if ((++i) == 15) {        /* output 60 characters? */
  1395.       i = 0;            /* restart line break count, insert CRLF */
  1396.       *d++ = '\015'; *d++ = '\012';
  1397.     }
  1398.   }
  1399.   *d++ = '\015'; *d++ = '\012';    /* insert final CRLF */
  1400.   *d = '\0';            /* tie off string */
  1401.   if ((d - ret) != *len) fatal ("rfc822_binary logic flaw");
  1402.   return ret;            /* return the resulting string */
  1403. }
  1404.  
  1405. /* Convert QUOTED-PRINTABLE contents to 8BIT
  1406.  * Accepts: source
  1407.  *        length of source
  1408.  *         pointer to return destination length
  1409.  * Returns: destination as 8-bit text
  1410.  */
  1411.  
  1412. unsigned char *rfc822_qprint (unsigned char *src,unsigned long srcl,
  1413.                   unsigned long *len)
  1414. {
  1415.   unsigned char *ret = (unsigned char *) fs_get (srcl);
  1416.   unsigned char *d = ret;
  1417.   unsigned char *s = d;
  1418.   unsigned char c,e;
  1419.   *len = 0;            /* in case we return an error */
  1420.   src[srcl] = '\0';        /* make sure string tied off */
  1421.   while (c = *src++) {        /* until run out of characters */
  1422.     switch (c) {        /* what type of character is it? */
  1423.     case '=':            /* quoting character */
  1424.       switch (c = *src++) {    /* what does it quote? */
  1425.       case '\0':        /* end of data */
  1426.     src--;            /* back up pointer */
  1427.     break;
  1428.       case '\015':        /* non-significant line break */
  1429.     if (*src == '\012') src++;
  1430.     break;
  1431.       default:            /* two hex digits then */
  1432.     if (!isxdigit (c)) {    /* must be hex! */
  1433.       fs_give ((void **) &ret);
  1434.       return NIL;
  1435.     }
  1436.     if (isdigit (c)) e = c - '0';
  1437.     else e = c - (isupper (c) ? 'A' - 10 : 'a' - 10);
  1438.     c = *src++;        /* snarf next character */
  1439.     if (!isxdigit (c)) {    /* must be hex! */
  1440.       fs_give ((void **) &ret);
  1441.       return NIL;
  1442.     }
  1443.     if (isdigit (c)) c -= '0';
  1444.     else c -= (isupper (c) ? 'A' - 10 : 'a' - 10);
  1445.     *d++ = c + (e << 4);    /* merge the two hex digits */
  1446.     s = d;            /* note point of non-space */
  1447.     break;
  1448.       }
  1449.       break;
  1450.     case ' ':            /* space, possibly bogus */
  1451.       *d++ = c;            /* stash the space but don't update s */
  1452.       break;
  1453.     case '\015':        /* end of line */
  1454.       d = s;            /* slide back to last non-space, drop in */
  1455.     default:
  1456.       *d++ = c;            /* stash the character */
  1457.       s = d;            /* note point of non-space */
  1458.     }      
  1459.   }
  1460.   *d = '\0';            /* tie off results */
  1461.   *len = d - ret;        /* calculate length */
  1462.   return ret;            /* return the string */
  1463. }
  1464.  
  1465. /* Convert 8BIT contents to QUOTED-PRINTABLE
  1466.  * Accepts: source
  1467.  *        length of source
  1468.  *         pointer to return destination length
  1469.  * Returns: destination as quoted-printable text
  1470.  */
  1471.  
  1472. #define MAXL 75            /* 76th position only used by continuation = */
  1473.  
  1474. unsigned char *rfc822_8bit (unsigned char *src,unsigned long srcl,
  1475.                 unsigned long *len)
  1476. {
  1477.   unsigned long lp = 0;
  1478.   unsigned char *ret = (unsigned char *) fs_get (3*srcl + srcl/MAXL + 2);
  1479.   unsigned char *d = ret;
  1480.   char *hex = "0123456789ABCDEF";
  1481.   unsigned char c;
  1482.   while (srcl--) {        /* for each character */
  1483.                 /* true line break? */
  1484.     if (((c = *src++) == '\015') && (*src == '\012') && srcl) {
  1485.       *d++ = '\015'; *d++ = *src++; srcl--;
  1486.       lp = 0;            /* reset line count */
  1487.     }
  1488.     else {            /* not a line break */
  1489.                 /* quoting required? */
  1490.       if (iscntrl (c) || (c == 0x7f) || (c & 0x80) || (c == '=') ||
  1491.       ((c == ' ') && (*src == '\015'))) {
  1492.     if ((lp += 3) > MAXL) {    /* yes, would line overflow? */
  1493.       *d++ = '='; *d++ = '\015'; *d++ = '\012';
  1494.       lp = 3;        /* set line count */
  1495.     }
  1496.     *d++ = '=';        /* quote character */
  1497.     *d++ = hex[c >> 4];    /* high order 4 bits */
  1498.     *d++ = hex[c & 0xf];    /* low order 4 bits */
  1499.       }
  1500.       else {            /* ordinary character */
  1501.     if ((++lp) > MAXL) {    /* would line overflow? */
  1502.       *d++ = '='; *d++ = '\015'; *d++ = '\012';
  1503.       lp = 1;        /* set line count */
  1504.     }
  1505.     *d++ = c;        /* ordinary character */
  1506.       }
  1507.     }
  1508.   }
  1509.   *d = '\0';            /* tie off destination */
  1510.   *len = d - ret;        /* calculate true size */
  1511.                 /* try to give some space back */
  1512.   fs_resize ((void **) &ret,*len);
  1513.   return ret;
  1514. }
  1515.